package com.jprocms.module.cms.util;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CompletableFutureUtils {
    private static final ScheduledExecutorService scheduler =
            Executors.newScheduledThreadPool(1, r -> {
                Thread t = new Thread(r);
                t.setDaemon(true); // 设置为守护线程（防止阻止JVM退出）
                return t;
            });

    public static CompletableFuture<Void> runAfterDelay(Runnable task, long delay, TimeUnit unit) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        scheduler.schedule(() -> {
            try {
                task.run();
                future.complete(null);
            } catch (Exception e) {
                future.completeExceptionally(e);
            }
        }, delay, unit);
        return future;
    }

    // 关闭线程池（按需调用）
    public static void shutdown() {
        scheduler.shutdown();
    }
}